package org.andes.kownledge;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import lisp.SExp;

import org.andes.help.struct.IContent;
import org.armedbear.lisp.LispObject;

/**
 * Created by IntelliJ IDEA.
 * User: wenfeix
 * Date: 2011-1-31
 * Time: 23:59:55
 * To change this template use File | Settings | File Templates.
 */
public class Problem implements IContent {
    public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getModdate() {
		return Moddate;
	}
	public void setModdate(String moddate) {
		Moddate = moddate;
	}
	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	}
	public String getStatement() {
		return statement;
	}
	public void setStatement(String statement) {
		this.statement = statement;
	}
	public String getTimes() {
		return Times;
	}
	public void setTimes(String times) {
		Times = times;
	}
	public String getChoices() {
		return Choices;
	}
	public void setChoices(String choices) {
		Choices = choices;
	}
	public String getGraphic() {
		return Graphic;
	}
	public void setGraphic(String graphic) {
		Graphic = graphic;
	}
	public String getPredefs() {
		return Predefs;
	}
	public void setPredefs(String predefs) {
		Predefs = predefs;
	}
	public String getComments() {
		return Comments;
	}
	public void setComments(String comments) {
		Comments = comments;
	}
	public String getFeatures() {
		return Features;
	}
	public void setFeatures(String features) {
		Features = features;
	}
	public String getForbiddenPSMS() {
		return ForbiddenPSMS;
	}
	public void setForbiddenPSMS(String forbiddenPSMS) {
		ForbiddenPSMS = forbiddenPSMS;
	}
	public String getIgnorePSMS() {
		return IgnorePSMS;
	}
	public void setIgnorePSMS(String ignorePSMS) {
		IgnorePSMS = ignorePSMS;
	}
	public String getVariableMarks() {
		return VariableMarks;
	}
	public void setVariableMarks(String variableMarks) {
		VariableMarks = variableMarks;
	}
	public String getWorkingMemory() {
		return WorkingMemory;
	}
	public void setWorkingMemory(String workingMemory) {
		WorkingMemory = workingMemory;
	}
	public List<qvar> getVarIndex() {
		return VarIndex;
	}
	public void setVarIndex(List<qvar> varIndex) {
		VarIndex = varIndex;
	}
	public Solution getSolution() {
		return solution;
	}
	public void setSolution(Solution solution) {
		this.solution = solution;
	}
	public void setGivens(List<LispObject> givens) {
		this.givens = givens;
	}
	public void setSoughts(LispObject[] soughts) {
		this.soughts = soughts;
	}
	private String Name;
    private String Moddate;
    private int version;
    private String statement;
    private String Times;
    
    private String Choices;
    private String Graphic;
    private String Predefs;
    private String Comments;
    private String Features;
    
    private String ForbiddenPSMS;
    private String IgnorePSMS;
    private String VariableMarks;
    private String WorkingMemory;
 
    private List<LispObject> givens=new ArrayList<LispObject>();
    public void addGiven(LispObject given){
    	givens.add(given);    	
    }
    public List<LispObject> getGivens(){
    	return givens;
    }
    private LispObject[] soughts;
   
    public LispObject[] getSoughts(){
    	return soughts;
    }
    
    private BubbleGraph graph;
    private List<qvar> VarIndex=new ArrayList<qvar>();
    public List<qvar> VarIndex(){
        return  VarIndex;
    }
    private List<eqn> EqnIndex=new ArrayList<eqn>();
    public List<eqn> EqnIndex(){
        return EqnIndex;
    }
    private  Solution solution;
    public Solution solution(){
        return solution;
    }


    public void openProblemFile(String fileName) throws Exception{
    	BufferedInputStream   in   =   new   BufferedInputStream(   new   FileInputStream(   fileName   )   ); 
    	readProblemFile(in);
    }
   
    public void readContent(String s) {
       //SExp.INPUT();
    }
    public  void readProblemFile(InputStream S) throws Exception {
        if(mpf_read(S,"<Andes2Problem>"))  {
        	readContent(S);
        }
    }
    

	public void readContent(InputStream S) throws Exception {
		SExp sexp = SExp.INPUT(S);
		String name = sexp.GetValue();
		if (!"</Andes2Problem>".equals(sexp.GetValue())) {
			set_pfile_contents(name, S);
			readContent(S);
		}else
			return;
	}

    private void set_pfile_contents(String name, InputStream S)throws Exception {
        if(name.equals("Name")){
            Name=SExp.INPUT(S).GetValue() ;
        } else if(name.equals("Version")){
            version=Integer.valueOf(SExp.INPUT(S).GetValue()).intValue() ;
        }  else if(name.equals("Moddate")){
            Moddate=SExp.INPUT(S).GetValue() ;
        } else if(name.equals("Statement")){
        	statement=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Times")){
        	Times=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Choices")){
        	Choices=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Graphic")){
        	Graphic=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Predefs")){
        	Predefs=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Comments")){
        	Comments=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Features")){
        	Features=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("ForbiddenPSMS")){
        	ForbiddenPSMS=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("IgnorePSMS")){
        	IgnorePSMS=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("VariableMarks")){
        	VariableMarks=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("WorkingMemory")){
        	WorkingMemory=SExp.INPUT(S).GetValue() ;
        }else if(name.equals("Bubblegraph")){
            graph=new BubbleGraph();
            graph.readContent(S);
        } else if(name.equals("EqnIndex")){
            readEqnIndex(S,graph);
        }else if(name.equals("VarIndex")){            
           readVarIndex(S,graph);
        } else if(name.equals("Solutions")){
            solution=new Solution();
            solution.readContent(S,graph,EqnIndex,version);
        }

    }

    public void readEqnIndex(InputStream S,BubbleGraph g) throws Exception {
         SExp sexp=SExp.INPUT(S);
        List<SExp> tempList= sexp.toList();
        for(SExp exp:tempList){
            eqn eqn=new eqn();
            if(!exp.IsNull()){
            	 eqn.readContent(exp,g);
                 EqnIndex.add(eqn);
            }
           

        }
    }
    public void readVarIndex(InputStream S,BubbleGraph g) throws Exception {
        SExp sexp=SExp.INPUT(S);
        List<SExp> tempList= sexp.toList();
        for(SExp exp:tempList){
            qvar qvar=new qvar();
            if(!exp.IsNull()){
            	qvar.readContent(exp,g);
                VarIndex.add(qvar);
           }
            

        }
    }
    /*
       (defmacro mpf-read (S C)
  `(when (not (equalp (read ,S) ,C))
     (error "Malformed Problem file.")))
     */
    public boolean mpf_read(InputStream S,String C) throws Exception {
         SExp sexp=SExp.INPUT(S);
         if(C.equals(sexp.GetValue()))
             return true;
        else
            return false;
             

    }

    public BubbleGraph getGraph() {
        return graph;
    }

    public void setGraph(BubbleGraph graph) {
        this.graph = graph;
    }

    public boolean no_quant_problem_p(){
        return false;
    }
    
    
    
}
